/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.tool.setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

import top.wardyan.base.action.BaseShell;
import top.wardyan.base.util.WTool;

/**
 * 
 * @author ward 2019年5月1日
 */
public class Setter extends BaseShell<Setter>{
	protected Text text;
	protected Button btn;
	
	public Setter() {
		createContents();
	}
	
	
	
	
	@Override
	protected String getTitle() {
		return "GETTER/SETTER生成";
	}

	private SelectionAdapter click = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			String[] lines = self.text.getText().split(System.lineSeparator());
			Integer index = 1;
			self.result.setText("");
			
			String txt = null;
			for(String s : lines) {
				
				if(s.length() < 1) {
					index ++;
					continue;
				}
				
				if(index.equals(1)) {
					txt = "public class " + s + " extends BaseModel<" + s + ">{" + System.lineSeparator();
					txt += "	public static final " + s + " dao = new " + s + "().dao();";
				}else if(index.equals(2)) {
					//public static final String TABLE = "card";
					txt = "	public static final String TABLE = \"" + s + "\"";
				}else if(index.equals(3)) {
					//public static final String PRIMARYKEY = "ID";
					txt = "	public static final String PRIMARYKEY = \"" + s + "\"";
				}else if(index.equals(lines.length)) {
					txt = "}";
				}else {
					String type = s.substring(0, 1);
					String field = s.substring(2);
					txt = "	public ";
					switch(type) {
						case "i":
							txt += "Integer";
							break;
						case "b":
							txt += "Boolean";
							break;
						case "f":
							txt += "Float";
							break;
						case "s":
							txt += "String";
							break;
						case "t":
							txt += "java.sql.Timestamp";
							break;
					}
					txt += " get";
					txt += field.substring(0, 1).toUpperCase() + field.substring(1) + "(){";
					txt += "	return this.get";
					switch(type) {
						case "i":
							txt += "Int";
							break;
						case "b":
							txt += "Boolean";
							break;
						case "f":
							txt += "Float";
						case "s":
							txt += "Str";
							break;
						case "t":
							txt += "Timestamp";
							break;
					}
					txt += "(\"" + field + "\");	}" + System.lineSeparator();
					txt += "	public M set";
					txt += field.substring(0, 1).toUpperCase() + field.substring(1) + "(";
					switch(type) {
						case "i":
							txt += "Integer";
							break;
						case "b":
							txt += "Boolean";
							break;
						case "f":
							txt += "Float";
						case "s":
							txt += "String";
							break;
						case "t":
							txt += "java.sql.Timestamp";
							break;
					}
					txt += " " + field + "){" + System.lineSeparator();
					txt += "		this.set(\"" + field + "\", " + field + ");"+ System.lineSeparator();
					txt += "		return (M) this;" + System.lineSeparator();
					txt += "	}" + System.lineSeparator();
				}
				
				
				self.result.setText(self.result.getText() + txt + System.lineSeparator());
				index ++;
			}
			WTool.setSysClipboardText(self.result.getText());
		}
	};
	
	
	protected Text result;


	private void createContents() {
		//this.setBounds(0, 0, width, height);
		//this.setLocation(0, 0);
		//this.setSize(Display.getCurrent().getClientArea().width, Display.getCurrent().getClientArea().height);
		//this.setMaximized(true);
		
		this.text = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		//this.text.setBounds(10, 71, 73, 23);
		
		this.text.setLocation(10, 10);
		this.text.setSize(this.getClientArea().width - 20,  (this.getClientArea().height - 50) / 2);
		
		this.result = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		this.result.setLocation(10, this.text.getLocation().y + this.text.getSize().y + 10);
		this.result.setSize(this.getClientArea().width - 20,  (this.getClientArea().height - 50) / 2);
		
		this.btn = new Button(this, SWT.NONE);
		this.btn.setText("生成");
		this.btn.setBounds(10, this.getClientArea().height - 40, 80, 40);
		this.btn.addSelectionListener(click);
		
	}
	
	
	public static void main(String[] args) {
		new Setter().go();
	}
}
